package com.lostVip.app.data.service;

import java.sql.Connection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.lostVip.app.data.DbBuilder;
import com.lostVip.app.data.util.DbConnUtil;


public class DoctorManager {
	
	public static void main(String[] args){
		//打开链接
		Connection connSqlserver  = DbConnUtil.createConnSrc();
		Connection connMysql      = DbConnUtil.createConnDist();
		
		DbBuilder sqlserver = new DbBuilder(connSqlserver);
		DbBuilder mysql = new DbBuilder(connMysql);
		
		
		DoctorManager m = new DoctorManager();
		m.copy_HisBaseUser(mysql,sqlserver);
		
		//关闭
		DbConnUtil.close(connSqlserver);
		DbConnUtil.close(connMysql);
	}
	
	private void copy_HisBaseUser(DbBuilder mysql,DbBuilder sqlserver) {
		try{
			String sql = "Select * from his_base_user";
			List<Map<String, Object>> list = sqlserver.getListMap(sql,null);
            for(Map<String, Object> map:list){//遍历行
            	insert(mysql,"his_base_user",map);
			}
		}catch(Exception e){
			e.printStackTrace();
		}
	}

	private void insert(DbBuilder mysql,String table, Map<String, Object> map) {
		StringBuilder sb = new StringBuilder();
		StringBuilder fileds = new StringBuilder();
		StringBuilder values     = new StringBuilder();
		
		Iterator<String> it = map.keySet().iterator();
		while(it.hasNext()){
			String key = it.next();
			Object o = map.get(key);
		    
			fileds.append(key).append(",");
			values.append("'").append(o).append("'").append(",");
		}
		fileds.replace(fileds.length()-1, fileds.length(), "");
		values.replace(values.length()-1, values.length(), "");
		
		sb.append(" insert into ").append(table).append(" (").append(fileds).append(") values (").append(values).append(")");
		try{
			mysql.save(sb.toString());
			System.out.println("成功："+sb);
		}catch(Exception e){
			e.printStackTrace();
			System.out.println("失败："+sb);
		}
		
	}

	

}
